{{if false}}<script>{{/if}}
{{$controller = $smarty.const.CCTRL}}
{{$token = $smarty.const.TOKEN}}
{{include file="../00_base/function.tpl"}}
function handleEvents(){
    try{
        acolumns['UnitPrice'].createeditor=function (row, cellvalue, editor) {
            editor.jqxNumberInput({ decimalDigits: 2 });
        };
        acolumns['UnitPrice'].validation= function (cell, value) {
            if (value < 0 || value > 1000000) {
                return { result: false, message: "Unit Price should be in the 0-1,000,000 interval" };
            }
            return true;
        };
        myGrid.bind('cellendedit', function (event) {
            try{
                var args = event.args;
                var _column = args.datafield ,_row = args.rowindex,_value = args.value;
                var _data=myGrid.jqxGrid('getrowdata', _row);
                var _id=_data.ID;
                if(_id==undefined || _id==""){
                    return;
                }
                switch(_column){
                    case "Status":
                        if(_value=="true" || _value ==true) _value="Public";
                        else _value="Private";
                        updateCol(_column,_id,_value);
                        break;
                    case "PackageName":
                        if(_value!=_data.Title)updateCol(_column,_id,_value);
                        break;
                    case "UnitPrice":
                        if(_value!=_data.Title)updateCol(_column,_id,_value);
                        break;
                    default:
                        uiMessage("Unkown this cell. Maybe 'editable' of this colums is 'off'."); 
                }
            }catch(e){
                uiMessage(e.message);
                console.log("Error:"+e.message);
            }
        });
    }catch(e){tipMessage(e.message);}
}
function SaveItem(){
    try{
        if(isrunning)return;
        console.log("Save ↵ Call");
        var _productnums =$(".AlbumItem input.num").map(function() {
            return $(this).val();
        });
        var _productids =$(".AlbumItem input.productid").map(function() {
            return $(this).val();
        });
        var _products=new Array();
        for(var i=0;i<_productids.length;i++){
            _products.push(new Array(_productids[i],_productnums[i]));
        }
        var url="{{base_url()}}backend/{{$controller}}/save";
        var data={
            _id             :   $("#txtID").val(),
            _name           :   $("#txtPackageName").val(),
            _alias          :   $("#txtAlias").val(),
            _unitprice      :   $("#txtUnitPrice").val(),
            _description    :   tinyMCE.get('txtDescription').getContent(),
            _products       :   _products
        }
        //ShowNoticeDialogMessage($("<div />").text(Content).html());return;
        console.log(data);
        isrunning=true;
        myAjax(url,data,function(data){
            isrunning=false;
            if(data.result>=0){
                uiMessage(data.message)
                myjqxGrid.CancelEdit();
                myjqxGrid.Refresh();
            }else{
                ShowErrorDialogMessage(data.message);
            }
        });
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
function DelProductItem(t){
    try{
    var e=$(t).parents(".AlbumItem");
    var id=e.find("input.productid").val();
    var name=e.find("div.productname:first-child").html();
    var item={ProductID:id,ProductName:name}
    console.log(item);
    $("#txtProductToken").tokenInput("remove", item);
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
function AddProductItem(item){
    try{
    var AlbumItems =$("#AlbumItems");
    var num=$(".Pr_"+ item.ProductID +" input.num:first-child").val();
    if(num==undefined)num=1;
    else{
        $(".Pr_"+item.ProductID+" input.num:first-child").val(parseInt(num)+1);
        return;
    }
    var AlbumItem=
    '<div class="AlbumItem Pr_'+item.ProductID+' grid_5 mb4 mt4 ml4 mr4 bdccc gadiend-white">\
        <div class="pt1 pb1 pl1 pr1">\
            <div class="pl8 pt4 pb4 pr8 ovfh mt0 mb0 mr0 ml0 bdbccc productname">'+item.ProductName+'</div>\
            <div class="pa r8 t4">\
                <span class="hover50" style="cursor: pointer" title="Delete this item" onclick="DelProductItem(this)">✖</span>\
            </div>\
            <div class="pl4 pr4 pt4 pb4 mt0 mb0 ml0 mr0 ovfh pr" style="height: 200px">\
                <img title="$'+item.UnitPrice+'" class="bgloadding32" alt="thumb" src="'+item.Image+'"/>\
            </div>\
            <div class="pr10">\
                <input class="classic-input w100pc num" type="number" min="1"\
                        onchange="getPackageCash()"\
                        title="Product number"\
                        value="'+num+'"/>\
                <input type="hidden" class="hidden productid" value="'+item.ProductID+'"/>\
                <input type="hidden" class="hidden unitprice" value="'+item.UnitPrice+'"/>\
            </div>\
        </div>\
    </div>\
    ';
    AlbumItems.append(AlbumItem);
    getPackageCash();
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
function getPackageCash(){
    try{
    var _cashs=0;
    $(".AlbumItem").map(function() {
        var _price = parseFloat($(this).find("input.unitprice").val());
        var _num = parseInt($(this).find("input.num").val());
        _cashs += _num*_price;
    });
    $("#txtUnitPrice").val(_cashs.toFixed(2));
    uiMessage("<tt>Unit Price</tt> have been change to $"+_cashs.toFixed(2));
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
